package com.liwenfeng.file;

import java.util.Arrays;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Predicate;

/*
* 斐波那契数列的两种实现方法
* 1。递归法
* 2。数组法
* */
public class FibonacciSequence {
    //递归法计算
    public int f(int num) {
        if (num > 2) {
            return f(num - 1) + f(num - 2);
        } else {
            return 1;
        }
    }

    //普通法
    public int arrayMethod(int n) {
        if (n <= 2) return 1;
        int[] ints = new int[n];
        for (int i = 0; i < ints.length; i++) {
            if (i < 2) {
                ints[i] = 1;
            } else {
                ints[i] = ints[i - 1] + ints[i - 2];
            }
        }
        System.out.println(Arrays.toString(ints));
        return ints[n - 1];
    }

    public static void main(String[] args) {
        FibonacciSequence sequence = new FibonacciSequence();
//        ArrayList<Integer> list = new ArrayList<>();
//        for (int i = 1; i <= 20; i++) {
//            list.add(sequence.f(i));
//        }
//        System.out.println(list);
        System.out.println(sequence.arrayMethod(10));

    }

}
